package com.zfzs.post.utils;

import com.zfzs.post.enums.DayType;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;

/**
 * 日期工具类
 *
 * @author Jalf
 * @ClassName: DateUtils
 * @since 2016年6月2日 下午2:44:02
 * Copyright  foxtail All right reserved.
 */
public class DateStrUtils {
    public static final String Y = "yyyy";

    public static final String YMD = "yyyyMMdd";

    public static final String YMDHMS = "yyyyMMdd HH:mm:ss";

    public static final String Y_M_D = "yyyy-MM-dd";

    public static final String Y_M_DHMS = "yyyy-MM-dd HH:mm:ss";

    public static final String YMDHMSS = "yyyyMMddhhmmssSSS";

    public static final String Y_M = "yyyy-MM";


    /**
     * Description:根据毫秒数转换为时间
     *
     * @param millseconds
     * @return Copyright  foxtail All right reserved.
     * @Title: getTime
     * @author Jalf
     * @since 2016年6月2日 下午2:45:20
     */
    public static String getTime(Long millseconds) {
        String time = "";
        if (millseconds == null) {
            return "";
        }
        int days = (int) millseconds.longValue() / 1000 / 60 / 60 / 24;
        if (days > 0) {
            time = time + days + "天";
        }
        long hourMillseconds = millseconds.longValue() - days * 1000 * 60 * 60 * 24;
        int hours = (int) hourMillseconds / 1000 / 60 / 60;
        if (hours > 0) {
            time = time + hours + "小时";
        }
        long minuteMillseconds = millseconds.longValue() - days * 1000 * 60 * 60 * 24 - hours * 1000 * 60 * 60;

        int minutes = (int) minuteMillseconds / 1000 / 60;
        if (minutes > 0) {
            time = time + minutes + "分钟";
        }

        long secondMillseconds = millseconds.longValue() - days * 1000 * 60 * 60 * 24 - hours * 1000 * 60 * 60 - minutes * 1000 * 60;
        int second = (int) secondMillseconds / 1000;
        if (second > 0) {
            time = time + second + "秒";
        }
        return time;
    }

    /**
     * Description:获取当前时间字符串
     *
     * @return Copyright  foxtail All right reserved.
     * @Title: getCurrTimeForString
     * @author Jalf
     * @since 2016年6月2日 下午2:46:24
     */
    public static String getCurrTimeForString() {
        return formatDate(new Date(), Y_M_DHMS);
    }

    public static String getCurrDayForString() {
        return formatDate(new Date(), Y_M_D);
    }


    /**
     * Description:获取当前日期字符串
     *
     * @return Copyright  foxtail All right reserved.
     * @Title: getCurrDateForString
     * @author Jalf
     * @since 2016年6月2日 下午2:46:47
     */
    public static String getCurrDateForString() {
        return formatDate(new Date(), YMD);
    }

    /**
     * Description:格式化日期成字符串
     *
     * @param date
     * @param pattern
     * @return Copyright  foxtail All right reserved.
     * @Title: formatDate
     * @author Jalf
     * @since 2016年6月2日 下午2:47:29
     */
    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * Description:将格式日期的字符串解析成Date
     *
     * @param dateStr
     * @param pattern
     * @return Copyright  foxtail All right reserved.
     * @Title: parseDate
     * @author Jalf
     * @since 2016年6月2日 下午2:47:55
     */
    public static Date parseDate(String dateStr, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getYesterdayStr(String format) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat(format).format(cal.getTime());
    }

    /**
     * 获取以当月为基础偏移的月份
     * e.g current :2019-10
     * -1 :2019-09
     *
     * @param format 格式
     * @param offset 偏移量
     * @return
     */
    public static String getMonthOffsetStr(String format, Integer offset) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, offset);
        return new SimpleDateFormat(format).format(cal.getTime());
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 获取昨天日期
     */
    public static String getYesterDay() {
        Calendar calendar = Calendar.getInstance();
        System.out.println(Calendar.DATE);
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(time);
        return yesterday;
    }

    /**
     * 当前月第一天
     *
     * @return
     */
    public static String getMinMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());

    }

    /**
     * 获取当前月份最后一天
     */
    public static String getMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    /**
     * @Author: umizhang
     * @Title: getStartOrEndDayOfQuarter
     * @Description TODO 获取本季度的第一天或最后一天
     * @Date: 2019/7/23 13:46
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    /**
     * @Author: umizhang
     * @Title: getStartOrEndOfYear
     * @Description TODO 获取本年的第一天或最后一天
     * @Date: 2019/7/23 13:48
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    /**
     * @Author: umizhang
     * @Title: getStartOrEndDayOfMonth
     * @Description TODO 获取本月的第一天或最后一天
     * @Date: 2019/7/23 11:50
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        int length = month.length(today.isLeapYear());
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), month, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), month, length);
        }
        return resDate.toString();
    }

    /**
     * @Author: umizhang
     * @Title: getFirstDayOfWeek
     * @Description TODO 获取本周的第一天或最后一天
     * @Date: 2019/7/23 10:59
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        if (isFirst) {
            resDate = today.minusDays(value - 1);
        } else {
            resDate = today.plusDays(7 - value);
        }
        return resDate.toString();
    }

    /**
     * 个悲剧时间类型获取起止日期
     *
     * @param type 日期类型
     */
    public static Map<String, String> getStartAndEndDate(String type) {
        Map<String, String> map = new HashMap<>();
        //开始日期
        String startDay = "";
        //结束日期
        String endDate = "";
        switch (type) {
            case DayType.YESTER_DAY:
                startDay = getYesterDay();
                endDate = getYesterDay();
                break;
            case DayType.TO_DAY:
                startDay = getCurrDayForString();
                endDate = getCurrDayForString();
                break;
            case DayType.TO_MONTH:
                startDay = getMinMonthDate();
                endDate = getMaxMonthDate();
                break;
            case DayType.TO_SEASON:
                startDay = getStartOrEndDayOfQuarter(LocalDate.now(), true);
                endDate = getStartOrEndDayOfQuarter(LocalDate.now(), false);
                break;
            case DayType.TO_YEAR:
                startDay = getStartOrEndDayOfYear(LocalDate.now(), true);
                endDate = getStartOrEndDayOfYear(LocalDate.now(), false);
                break;
            default:
                return null;
        }
        map.put("startDate", startDay);
        map.put("endDate", endDate);
        return map;
    }

    public static void main(String[] args) {
        System.out.println(getStartOrEndDayOfQuarter(LocalDate.now(), false));
    }

}